package com.suse.common.config.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一返回结果对象
 */
@ApiModel(value = "统一返回结果对象")
@Data
@NoArgsConstructor
public class R {

    /**
     * 是否成功
     */
    @ApiModelProperty(value = "是否成功")
    private boolean success;

    /**
     * 状态码
     */
    @ApiModelProperty(value = "状态码")
    private Integer code;

    /**
     * 信息
     */
    @ApiModelProperty(value = "信息")
    private String message;

    /**
     * 数据
     */
    @ApiModelProperty(value = "数据")
    private Object data;

    private R(boolean success,Integer code, String message, Object data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static MapBuilder put(String key, Object value) {
        MapBuilder builder = new MapBuilder();
        builder.put(key,value);
        return builder;
    }

    public static R success() {
        return new R(true,200,null,null);
    }

    public static R success(Integer code, Object data) {
        return new R(true,code,null,data);
    }

    public static R success(String message) {
        return new R(true,200,message,null);
    }

    public static R success(Object data) {
        return new R(true,200,null,data);
    }

    public static R success(Object data, Code code) {
        return new R(true,code.getCode(), code.getMessage(),data);
    }

    public static R success(Object data, String message) {
        return new R(true,200, message,data);
    }

    public static R success(Code code) {
        return new R(true,code.getCode(), code.getMessage(),null);
    }

    public static R error() {
        return new R(false,500,null,null);
    }

    public static R error(String message) {
        return new R(false, 500, message,null);
    }

    public static R error(Integer code, String message) {
        return new R(false,code, message,null);
    }

    public static R error(Code code) {
        return new R(false,code.getCode(), code.getMessage(),null);
    }

    public static R error(Code code, String message) {
        return new R(false,code.getCode(),message,null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

}
